package com.passive.lock.service.user.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.passive.lock.mapper.TMenuMapperCustom;
import com.passive.lock.mapper.TRoleMapper;
import com.passive.lock.mapper.TRoletomenuMapper;
import com.passive.lock.model.po.TMenu;
import com.passive.lock.model.po.TRole;
import com.passive.lock.model.po.TRoleExample;
import com.passive.lock.model.po.TRoletomenu;
import com.passive.lock.model.po.TRoletomenuExample;
import com.passive.lock.model.vo.RightVo;
import com.passive.lock.model.vo.TMenuVo;
import com.passive.lock.model.vo.TRoleVo;
import com.passive.lock.service.user.RoleService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.tree.TreeMenuUtil;
import com.passive.lock.utils.tree.TreeRightUtil;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
	@Resource(name = "TRoleMapper")
	private TRoleMapper roleMapper;

	@Resource(name = "TRoletomenuMapper")
	private TRoletomenuMapper roletomenuMapper;

	@Resource(name = "TMenuMapperCustom")
	private TMenuMapperCustom menuMapperCustom;

	@Override
	public Result roleList(String groupid) {

		TRoleExample example1 = new TRoleExample();
		TRoleExample.Criteria criteria1 = example1.createCriteria();
		criteria1.andGroupidEqualTo("admin");
		List<TRole> list1 = roleMapper.selectByExample(example1);
		List<TRoleVo> listvo = new ArrayList<>();
		for (TRole TRole1 : list1) {
			TRoleVo TRoleVo1 = new TRoleVo();
			DozerMapperUtils.map(TRole1, TRoleVo1);
			listvo.add(TRoleVo1);
		}
		TRoleExample example2 = new TRoleExample();
		TRoleExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andGroupidEqualTo(groupid);
		List<TRole> list2 = roleMapper.selectByExample(example2);
		for (TRole TRole2 : list2) {
			TRoleVo TRoleVo2 = new TRoleVo();
			DozerMapperUtils.map(TRole2, TRoleVo2);
			listvo.add(TRoleVo2);
		}
		return Result.ok(listvo);
	}

	@Override
	public Result addRole(TRoleVo tRoleVo) {
		TRole tRole = new TRole();
		DozerMapperUtils.map(tRoleVo, tRole);
		int count = roleMapper.insert(tRole);
		return Result.ok(count);
	}

	@Override
	public Result updateRole(TRoleVo tRoleVo) {
		TRole tRole = new TRole();
		DozerMapperUtils.map(tRoleVo, tRole);
		int count = roleMapper.updateByPrimaryKey(tRole);
		return Result.ok(count);
	}

	@Override
	public Result deleteRole(TRoleVo tRoleVo) {
		int count = roleMapper.deleteByPrimaryKey(tRoleVo.getId());
		TRoletomenuExample example = new TRoletomenuExample();
		TRoletomenuExample.Criteria criteria = example.createCriteria();
		criteria.andRoleidEqualTo(tRoleVo.getId());
		int counts = roletomenuMapper.deleteByExample(example);
		return Result.ok(count);
	}

	@Override
	public Result rightList(Integer roleId) {

		// 新需求转换功能
		/*
		 * TRoletomenuExample example = new TRoletomenuExample();
		 * TRoletomenuExample.Criteria criteria = example.createCriteria();
		 * criteria.andRoleidEqualTo(roleId); List<TRoletomenu> list =
		 * roletomenuMapper.selectByExample(example); List<Integer> menuIds =
		 * new ArrayList<Integer>(); if (CollectionUtils.isEmpty(list)) {
		 * List<TMenu> roleTreeList = menuMapperCustom.menuTreeList();
		 * List<TMenuVo> listvo = new ArrayList<>(); for (TMenu tMenu :
		 * roleTreeList) { TMenuVo TMenuVo = new TMenuVo();
		 * DozerMapperUtils.map(tMenu, TMenuVo); listvo.add(TMenuVo); }
		 * List<TMenuVo> newTreeDataList = TreeMenuUtil.getfatherNode(listvo);
		 * RightVo rightVo = new RightVo();
		 * rightVo.setRightTree(newTreeDataList); rightVo.setMenuIds(menuIds);
		 * return Result.ok(rightVo);
		 * 
		 * } else { for (TRoletomenu tRoletomenu : list) {
		 * menuIds.add(tRoletomenu.getMenuid()); } List<TMenuVo>
		 * roleTreeListByUserId =
		 * menuMapperCustom.menuTreeListByMenuIds(menuIds); List<TMenuVo>
		 * newTreeDataList = TreeRightUtil.getfatherNode(roleTreeListByUserId);
		 * RightVo rightVo = new RightVo();
		 * rightVo.setRightTree(newTreeDataList); rightVo.setMenuIds(menuIds);
		 * return Result.ok(rightVo); }
		 */
		List<Integer> menuIds = new ArrayList<Integer>();
		if (roleId != null) {
			TRoletomenuExample example = new TRoletomenuExample();
			TRoletomenuExample.Criteria criteria = example.createCriteria();
			criteria.andRoleidEqualTo(roleId);
			List<TRoletomenu> list = roletomenuMapper.selectByExample(example);

			for (TRoletomenu TRoletomenu : list) {
				menuIds.add(TRoletomenu.getMenuid());
			}
		}

		List<TMenu> roleTreeList = menuMapperCustom.menuList(null);
		List<TMenuVo> listvo = new ArrayList<>();
		for (TMenu tMenu : roleTreeList) {
			TMenuVo TMenuVo = new TMenuVo();
			DozerMapperUtils.map(tMenu, TMenuVo);
			listvo.add(TMenuVo);
		}
		List<TMenuVo> newTreeDataList = TreeRightUtil.getfatherNode(listvo);
		RightVo rightVo = new RightVo();
		rightVo.setRightTree(newTreeDataList);
		rightVo.setMenuIds(menuIds);
		return Result.ok(rightVo);

	}

	@Override
	public Result addRight(Integer roleId, List<Integer> menuIds) {
		TRoletomenuExample example = new TRoletomenuExample();
		TRoletomenuExample.Criteria criteria = example.createCriteria();
		criteria.andRoleidEqualTo(roleId);
		roletomenuMapper.deleteByExample(example);
		for (Integer menuid : menuIds) {
			TRoletomenu TRoletomenu = new TRoletomenu();
			TRoletomenu.setRoleid(roleId);
			TRoletomenu.setMenuid(menuid);
			roletomenuMapper.insert(TRoletomenu);
		}
		return Result.ok();
	}

}
